package com.jiangjiang.eshop.membership.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.membership.convert.DeliveryAddressConvert;
import com.jiangjiang.eshop.membership.dao.DeliveryAddressMapper;
import com.jiangjiang.eshop.membership.domain.DeliveryAddressEntity;
import com.jiangjiang.eshop.membership.dto.DeliveryAddressDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName DeliveryAddressService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/24 10:59
 * @Version 1.0
 **/

@Service
public class DeliveryAddressService extends ServiceImpl<DeliveryAddressMapper, DeliveryAddressEntity> {
    @Autowired
    private DeliveryAddressConvert deliveryAddressConvert;

    /**
     * 根据用户账户查询收货地址
     * @param userAccountId 用户账户编号
     * @return 查询结果
     */
    public List<DeliveryAddressDTO> listByUserAccountId(Long userAccountId) {
        LambdaQueryWrapper<DeliveryAddressEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DeliveryAddressEntity::getUserAccountId, userAccountId);
        return deliveryAddressConvert.convert2DTOList(this.list(wrapper));
    }

    /**
     * 新增用户地址
     * @param dto 入参
     * @return 处理结果
     */
    public Boolean saveAddress(DeliveryAddressDTO dto) {
        return this.save(deliveryAddressConvert.convertDTO2Entity(dto));
    }

    /**
     * 更新用户地址
     * @param dto 入参
     * @return 处理结果
     */
    public Boolean updateAddress(DeliveryAddressDTO dto) {
        return this.updateById(deliveryAddressConvert.convertDTO2Entity(dto));
    }
}
